<?php
declare (strict_types = 1);

namespace app\api\controller;

use think\facade\Db;

/**
 * @title 所有表内容管理
 * @description 接口说明
// * @header name:key require:1 default: desc:秘钥(区别设置)
// * @param name:public type:int require:1 default:1 other: desc:公共参数(区别设置)
 */
class Module extends Base
{


    /**
     * @title 获取指定分类列表
     * @description 接口说明：siteid ： 站点id，默认1 | moduleid ：模型id
     * @author 开发者
     * @url /api/Module/getClassifyList
     * @method GET POST
     *
     * @param name:siteid type:int require:0 default:1 other: desc:站点id，默认1
     * @param name:moduleid type:int require:0 default:1 other: desc:模型id
     *
     */
    public function getClassifyList(){
        $param = input("param.");
        $moduleid = $param['moduleid'];

        $list = DB::name("classify")
            ->where("moduleid","=",$moduleid)
            ->select();

        return dyajaxReturn(1,'获取成功',$list);
    }





    /**
     * @title 获取指定表的列表
     * @description 接口说明：siteid ： 站点id，默认1 | tablename ：表名 | isPage ：是否分页 1=分页，0=不分页 | page ：页码第几页 ###product表中 is_news=1新品，2=不是新品
     * @author 开发者
     * @url /api/Module/list
     * @method GET POST
     *
     * @param name:siteid type:int require:0 default:1 other: desc:站点id，默认1
     * @param name:cate_id type:int require:0 default:1 other: desc:栏目id，默认1
     * @param name:tablename type:varchar require:1 default:1 other: desc:表名
     * @param name:limit type:varchar require:0 default:10 other: desc:当页数量，默认10
     * @param name:isPage type:int require:0 default:1 other: desc:是否分页 1=分页，0=不分页
     * @param name:page type:int require:0 default:1 other: desc:第几页
     * @param name:keyword type:varchar require:0 default:1 other: desc:关键词搜索
     * @param name:is_price type:varchar require:0 default:asc other: desc:价格asc=正序，desc=反序
     * @param name:is_hits type:varchar require:0 default:asc other: desc:热度asc=正序，desc=反序
     * @param name:classifyid type:varchar require:0 default: other: desc:分类id
     * @param name:moduleid type:varchar require:0 default: other: desc:模型id 用于classify的查询
     *
     */
    public function list()
    {
        $param = input("param.");
        $siteid = isset($param['siteid'])?$param['siteid']:1;
        $isPage = isset($param['isPage'])?$param['isPage']:1;//是否要分页
        if( !isset($param['tablename'])){
            return dyajaxReturn(0,'表名不能为空');
        }

        $tablename = $param['tablename'];
        if (isset($param['limit'])) {
            $limit = $param['limit'];
        } else {
            $limit = 10;
        }
        $param['siteid'] = $siteid;
        $tableWhere = $this->tableWhere($param);
        $query      = DB::name($tablename)
            ->where($tableWhere['where'])->whereOr($tableWhere['whereOr'])->order($tableWhere['order']);

        if ($isPage) {
            $list        = $query->paginate($limit);
            $data        = $this->tableFormat($list->getCollection()->toArray());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $page = $list->render();
            $re['count'] = $list->total();
        } else {
            $list        = $query->select()->toArray();
            $data        = $this->tableFormat($list);         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $re['count'] = count($list);
            $page = [];
        }

        $re['data'] = $data;
        $re['page'] = $page;

        return dyajaxReturn(1,'获取成功',$re);
    }

    protected function tableWhere($post)
    {
        $where = $whereOr = [];

        if(isset($post['siteid']) && !empty($post['siteid'])){
            $where[] = ['siteid','=',$post['siteid']];
        }

        if(isset($post['cate_id']) && $post['cate_id'] != '' ){
            $where[] = ['catids','find in set',$post['cate_id']];
        }

        if(isset($post['keyword']) && $post['keyword'] != '' ){
            $where[] = ['title','like','%'.$post['keyword'].'%'];
        }

        if(isset($post['is_bao']) && $post['is_bao'] != '' ){
            $where[] = ['is_bao','=',$post['is_bao']];
        }

        if(isset($post['is_news']) && $post['is_news'] != '' ){
            $where[] = ['is_news','=',$post['is_news']];
        }

        if(isset($post['is_tui']) && $post['is_tui'] != '' ){
            $where[] = ['is_tui','=',$post['is_tui']];
        }

        if(isset($post['classifyid']) && $post['classifyid'] != '' ){
            $where[] = ['classifyid','=',$post['classifyid']];
        }

        if(isset($post['moduleid']) && $post['moduleid'] != '' ){
            $where[] = ['moduleid','=',$post['moduleid']];
        }

        $result['where']   = $where;
        $result['whereOr'] = $whereOr;

        $result['field'] = "*";

        if(isset($post['is_price']) && $post['is_price'] != '' ) {
            if ($post['is_price'] == 'asc') {
                $result['order'] = ['price' => 'asc'];
            }
            if ($post['is_price'] == 'desc') {
                $result['order'] = ['price' => 'desc'];
            }
        }else if(isset($post['is_hits']) && $post['is_hits'] != '' ){
            if ($post['is_hits'] == 'asc') {
                $result['order'] = ['hits' => 'asc'];
            }
            if ($post['is_hits'] == 'desc') {
                $result['order'] = ['hits' => 'desc'];
            }
        }else {
            $result['order'] = ['orderid' => 'asc', 'id' => 'desc'];
        }
        return $result;
    }

    protected function tableFormat($list)
    {
        foreach($list as $k=>$v){
            $list[$k]['add_time'] = date("Y-m-d H:i:s",$v['add_time']);
        }
        return $list;
    }


    /**
     * @title 获取指定表的详情
     * @description 接口说明：获取指定表的详情 info：当页详情，prev：上一页内容，next：下一页内容
     * @author 开发者
     * @url /api/Module/getDetails
     * @method GET POST
     *
     * @param name:siteid type:int require:0 default:1 other: desc:站点id，默认1
     * @param name:tablename type:varchar require:1 default:1 other: desc:表名
     * @param name:id type:int require:1 default:0 other: desc:栏目id
     */
    public function getDetails(){
        $param = input('param.');
        $siteid = isset($param['siteid'])?$param['siteid']:1;
        if( !isset($param['tablename'])){
            return dyajaxReturn(0,'表名不能为空');
        }
        if( !isset($param['id'])){
            return dyajaxReturn(0,'关键参数id不能为空');
        }
        $tablename = $param['tablename'];
        $id = $param['id'];

        $info = DB::name($tablename)->where([
            "siteid"=>$siteid,
            "id"=>$id
        ])->find();

        //点击自增一次
        DB::name($tablename)->where([
            "siteid"=>$siteid,
            "id"=>$id
        ])-> inc('hits') ->update();

        //上一页
        $prev  = DB::name($tablename)
            ->where("siteid","=",$siteid)
            ->where('id',"<>",$id)->where('orderid', '<',$info['orderid'])->order('orderid asc')->find();
        //下一页
        $next = DB::name($tablename)
            ->where("siteid","=",$siteid)
            ->where('id',"<>",$id)->where('orderid', '>',$info['orderid'])->order('orderid desc')->find();

        $arr = [
            'info'=>$info,
            'prev'=>$prev,
            'next'=>$next,
        ];

        return dyajaxReturn(1,'获取成功',$arr);
    }



}
